package com.freework.report.xmlreport.groupBean.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import com.freework.report.base.service.filterCondition.Condition;
import com.freework.report.base.vo.RequestParam;
import com.freework.report.xmlreport.groupBean.dao.CreateSQLBuiderBean;
import com.freework.report.xmlreport.groupBean.dao.CreateSQLBuiderBeanNotGroup;
import com.freework.report.xmlreport.groupBean.dao.SQLBuilderBean;
import com.freework.report.xmlreport.groupBean.vo.Col;
import com.freework.report.xmlreport.groupBean.vo.Cols;
import com.freework.report.xmlreport.groupBean.vo.Data;
import com.freework.report.xmlreport.groupBean.vo.Table;
import com.freework.util.MapUtil;
import com.freework.util.listUtil.group.AggregateCol;
import com.freework.util.listUtil.group.GroupCol;

public class CreateDataBean {
	Map<String,CreateSQLBuiderBean> map=MapUtil.asMap("01",new CreateSQLBuiderBean(),
													  "02",new CreateSQLBuiderBeanNotGroup());
	
	
	
	public GroupDataBean createGroupDataBean(Data data){
		SQLBuilderBean[] dbtabls=getSQLBuilderBeans(data.getTable(),data.getCols());
		AggregateCol[] cols=buildAggregateCol(data.getCols().getCol());
		GroupCol[] groupCol=buildGroupCol(data.getCols().getGroup().getCol());
		RequestParam[] requestParam=buildRequestParams(data.getTable());
		return new GroupDataBean(cols,groupCol,dbtabls,requestParam);
	}
	
	private SQLBuilderBean[] getSQLBuilderBeans(List<Table> tables,Cols cols){
		 SQLBuilderBean[] builder=new  SQLBuilderBean[tables.size()];
		for (int i = 0; i < builder.length; i++) {
			
			Table table=tables.get(i);
			builder[i]=map.get(table.getSqltype()).createBean(table, cols);
		}
		return builder;
	}
	 
	
	private  AggregateCol[] buildAggregateCol(List<Col> cols){
		ArrayList<AggregateCol> list=new ArrayList<AggregateCol>();
		HashSet<String> nameSet=new HashSet<String>();
			for (Col col : cols) {
				 String tagertName=col.getTagertName();
				// System.out.println(col.getSourceName()+"=="+col.getTable());
				 String type=col.getType();
				 if(!nameSet.contains(tagertName)){
					String sourceName=col.getSourceName();
					if(tagertName.indexOf('{')>=0&&tagertName.indexOf('}')>=0&&tagertName.indexOf(',')>=0)
						tagertName=tagertName.replace(',', '_');
					if(type!=null&&!type.equals("")){
						type=type.trim();
						sourceName=type+","+sourceName;
					}
					
					 list.add(new AggregateCol(sourceName,tagertName));
					 nameSet.add(tagertName);
				 }
		}
		return list.toArray(new AggregateCol[list.size()]);
	}
	
	private boolean contains(String colName,List<GroupCol> list){
		for (GroupCol groupCol : list) {
			if(groupCol.equals(colName))
				return true;
		}
		return false;
		
	}
	
	
	private GroupCol[] buildGroupCol(List<Col> groupCols){
		
		List <GroupCol> list=new ArrayList<GroupCol>(groupCols.size());
		
		for (int i = 0; i < groupCols.size(); i++) {
			Col col=groupCols.get(i);
			String colName=col.getTagertName();
			if(!contains(colName,list)){
				
				boolean isSum=col.isIssum();
				String splitType=col.getSplitType();
			
				int splitTypeInt[]=null;
				if(splitType!=null&&!splitType.equals("")){
					String splitTypeStr[]=splitType.split(",");
					splitTypeInt=new int[splitTypeStr.length];
					for (int j = 0; j < splitTypeInt.length; j++) {
						splitTypeInt[j]=Integer.parseInt(splitTypeStr[j]);
					}
				}
				
					list.add(new GroupCol(splitTypeInt,colName,i,isSum));
			}
		}
		//System.out.println(list);
		return list.toArray(new GroupCol[list.size()]);
	}
	private  RequestParam[]  buildRequestParams(List<Table> table){
		List<RequestParam> list=new ArrayList<RequestParam>();

		for (Table table2 : table) {
			List<Condition> conditions=table2.getCondition();
			for (Condition condition : conditions) {
				list.add(new RequestParam(condition.getScope(),condition.getParamName(),table2.getName()));
			}
		}
		return list.toArray(new RequestParam[list.size()]);
	}

	
}
